package org.example.wordcount;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * world count(dataStream )
 */
public class WordCountStream2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        全局设置并行度
        env.setParallelism(3);
        DataStreamSource<String> ds = env.readTextFile("aa");
        SingleOutputStreamOperator<Tuple2<String, Integer>> sum =
                ds.flatMap((String s, Collector<Tuple2<String, Integer>> collector) -> {
                            String[] split = s.split(" ");
                            for (String str : split) {
                                Tuple2<String, Integer> tuple2 = Tuple2.of(str, 1);
                                collector.collect(tuple2);
                            }
                        })
                        .returns(Types.TUPLE(Types.STRING, Types.INT))
//                        .keyBy((Tuple2<String, Integer> stringIntegerTuple2) -> stringIntegerTuple2.f0)
                        .keyBy(data -> data.f0)
                        .sum(1);
        sum.print();
        env.execute();

    }
}